<template>
  <div class="product-detail" v-if="productStore.currentProduct">
    <el-row :gutter="40">
      <!-- 商品图片 -->
      <el-col :span="12">
        <el-card class="product-image-card">
          <img :src="productStore.currentProduct.image" class="product-image" />
        </el-card>
      </el-col>
      
      <!-- 商品信息 -->
      <el-col :span="12">
        <el-card class="product-info-card">
          <h1 class="product-name">{{ productStore.currentProduct.name }}</h1>
          <div class="product-price">¥{{ productStore.currentProduct.price }}</div>
          <div class="product-description">
            {{ productStore.currentProduct.description }}
          </div>
          
          <div class="product-meta">
            <div class="meta-item">
              <el-icon><Tag /></el-icon>
              <span>分类：{{ productStore.currentProduct.category }}</span>
            </div>
            <div class="meta-item">
              <el-icon><Box /></el-icon>
              <span>库存：{{ productStore.currentProduct.stock }}</span>
            </div>
          </div>
          
          <div class="product-actions">
            <el-input-number
              v-model="quantity"
              :min="1"
              :max="productStore.currentProduct.stock"
              label="数量"
            />
            <el-button
              type="primary"
              size="large"
              @click="addToCart"
              :disabled="!userStore.token"
            >
              加入购物车
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useProductStore } from '../stores/product'
import { useUserStore } from '../stores/user'
import { useCartStore } from '../stores/cart'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const productStore = useProductStore()
const userStore = useUserStore()
const cartStore = useCartStore()
const quantity = ref(1)

onMounted(async () => {
  const productId = route.params.id
  await productStore.fetchProductById(productId)
})

const addToCart = async () => {
  if (!userStore.token) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }
  
  try {
    await cartStore.addToCart(productStore.currentProduct.id, quantity.value)
    ElMessage.success('已添加到购物车')
  } catch (error) {
    ElMessage.error('添加失败，请重试')
  }
}
</script>

<style scoped>
.product-detail {
  padding: 20px;
}

.product-image-card {
  height: 100%;
}

.product-image {
  width: 100%;
  height: 400px;
  object-fit: cover;
}

.product-info-card {
  height: 100%;
}

.product-name {
  font-size: 24px;
  margin-bottom: 20px;
}

.product-price {
  color: #f56c6c;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 20px;
}

.product-description {
  color: #666;
  margin-bottom: 30px;
  line-height: 1.6;
}

.product-meta {
  margin-bottom: 30px;
}

.meta-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  color: #666;
}

.meta-item .el-icon {
  margin-right: 8px;
}

.product-actions {
  display: flex;
  gap: 20px;
  align-items: center;
}

.product-actions .el-button {
  flex: 1;
}
</style> 